<template>
  <div>
    <div class="empty" v-if="data.length === 0">
      <img src="../../assets/image/icon/noOrder.png" alt="">
      <span>暂无订单</span>
    </div>
    <el-card class="box-card" v-for="item in data" :key="item.id" v-else>
      <!-- <el-row :gutter="18">
        <el-col :span="8">下单时间： {{moment(item.orderDt).format('YYYY-MM-DD HH:mm:ss')}}</el-col><el-col :span="8">订单号：{{item.orderNo}}</el-col>
      </el-row> -->
      <div class="con">
        <div>下单时间： {{moment(item.orderDt).format('YYYY-MM-DD HH:mm:ss')}}</div><div>订单号：{{item.orderNo}}</div>
      </div>
      <div class="box-title"><span>购买套餐包</span><span>商户名称</span><span>油站名称</span><span>原价（元）</span><span>订单状态</span></div>
      <div class="box-content"><span>{{item.packageName}}</span><span>{{item.companyName}}</span><span>{{item.gasName}}</span><span>{{item.priceOrigin}}</span><span>{{item.orderStatusDesc}}</span></div>
      <div class="money">
        <div class="detail"><span @click="detail(item.orderNo)">订单详情</span></div>
        <div class="cash">
          <span>订单金额：</span>
          <span>￥{{item.amountPay}}</span>
        </div>
      </div>
      <div class="btn" v-if="item.orderStatus === 1">
        <el-button size="middle" @click="cancelOrder(item.orderNo)">取消订单</el-button>
        <el-button size="middle" type="primary" @click="checkOrderApi(item.orderNo)">支付</el-button>
      </div>
    </el-card>
    <el-dialog title="订单详情" @closed="resetInfo=false" :center="true" width="600px" :visible.sync="dialogTableVisible">
      <orderDialog v-if="resetInfo" :orderNo="orderNo"></orderDialog>
    </el-dialog>
  </div>
</template>
<script>
import moment from 'moment'
import { getPackageOrderList, cancelOrder, checkOrder } from '@/api/market'
import orderDialog from './components/orderDialog'
export default {
  data () {
    return {
      orderNo: '',
      dialogTableVisible: false,
      resetInfo: true,
      data: []  // 订单列表数据
    }
  },
  components: {
    orderDialog
  },
  mounted () {
    this.getPackageOrderListApi()
  },
  methods: {
    moment,
    detail(orderNo) {
      this.orderNo = orderNo
      this.resetInfo = true
      this.dialogTableVisible = true
    },
    // 取消订单
    cancelOrder (orderNo) {
      this.$confirm('取消订单之后，您可重新下单购买套餐', '取消订单', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleCancelOrder(orderNo)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })  
      })
    },
    // 取消订单接口
    async handleCancelOrder (orderNo) {
      try {
        const { code } = await cancelOrder({ orderNo })
        this.getPackageOrderListApi()
        if (code !== 200) return
      } catch (err) {
        console.log(err)
      }
    },
    // 订单列表数据
    async getPackageOrderListApi () {
      try {
        const { code, result = [] } = await getPackageOrderList()
        if (code !== 200) return
        this.data = result
      } catch (err) {
        console.log(err)
      }
    },
    // 支付校验接口
    async checkOrderApi (orderNo) {
      try {
        const { code, message, result } = await checkOrder({ orderNo })
        if (code !== 200) return
        if (!result) {
          this.$message({ message: message, type: 'warning' })
          return
        }
        this.$router.push({ path: '/marketCenter/paymentOrder', query: { orderNo } })
      } catch (err) {
        console.log(err)
      }
    },
  }
}
</script>
<style scoped lang="less">
.box-card{
  margin-top: 20px;
  .con{
    display: flex;
    >div:nth-child(2) {
      margin-left:20px;
    }
  }
}
.box-title{
  width:100%;
  height:50px;
  margin-top:20px;
  background: #F5F7FA;
  border: 1px solid #EBEEF5;
  display: flex;
  justify-content: space-between;
  > span{
    font-size: 16px;
    color: #222222;
    width: 20%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
.box-content{
  width:100%;
  height:50px;
  border: 1px solid #EBEEF5;
  display: flex;
  justify-content: space-between;
  > span{
    width: 20%;
    padding: 0 10px;
    font-size: 16px;
    color: #222222;
    text-align: center;
    line-height: 48px;
    overflow:hidden;
    text-overflow:ellipsis;
    white-space:nowrap;
  }
}
.money{
  width: 100%;
  height: 60px;
  border-bottom: 1px solid #CCCCCC;
  display: flex;
  justify-content: space-between;
  >div{
    width: 20%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .detail {
    color: #4B7EFE;
    font-size: 16px;
    text-decoration : underline;
    cursor: pointer;
  }
  .cash {
    >span:nth-child(1){
      font-size: 16px;
      color: #999999;
    }
    >span:nth-child(2){
      font-size: 20px;
      color: #E31837;
    }
  }
}
.btn{
  padding-top: 20px;
  display: flex;
  justify-content: flex-end;
  .el-button{
    width:160px;
  }
}
.empty{
  width:100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  >img {
    display: block;
    width: 190px;
    height: 190px;
  }
  >span {
    margin-top:20px;
  }
}
</style>